package com.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.app.bean.Movies;
import com.app.bean.Orders;
import com.app.bean.Ticket;
import com.app.service.MovieService;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import static com.app.util.StringUtilsEx.*;

@Controller
@RequestMapping("/movie")
public class MovieController {

	@Autowired
	private MovieService ms;
	
	@RequestMapping("/listOrder")
	@ResponseBody
	public Map<String,Object> listOrder(Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<Orders> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			list = ms.listOrder();
			result.put("rows", list);
			PageInfo<Orders> pageInfo = new PageInfo<Orders>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
	//根据电影票ID生成订单
	@RequestMapping("/addOrder")
	@ResponseBody
	public Map<String,Object> addOrder(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			ms.addOrder(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Movies getById(String id){
		return	ms.getById(id);
	}
	
	@RequestMapping("/createTickets")
	@ResponseBody
	public Map<String,Object> createTickets(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		//先检查这个id的电影是否已经生成了电影票
		List<Ticket> tickets = ms.getTicketsByMovieId(id);
		
		if(tickets != null && !tickets.isEmpty()){
			returnVo.put("code", 2);
			return returnVo;
		}
		
		try{
			ms.createTickets(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String,Object> update(String id,String column){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			ms.update(id,column);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			ms.delete(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add(Movies movie){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			
			if(StringUtilsEx.isNotEmpty(movie.getId())){
				ms.update(movie);
			}else{
				 ms.add(movie);
			}
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String moviename,Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<Movies> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			list = ms.list(moviename);
			result.put("rows", list);
			PageInfo<Movies> pageInfo = new PageInfo<Movies>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
}
